<?php
header('Content-type: text/vnd.wap.wml');
echo '<?xml version="1.0" encoding="utf-8"?>'."\n";
?>
<!DOCTYPE wml PUBLIC "-//PHONE.COM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml">
<wml>
<card title="Kien's wap">
<p>
<?php
define('MAX_LINE',5);
define('SCREEN_WIDTH', 120);
/*
stupid code to split a long text to chunks of several(MAX_LINE) lines, any ideas?
in: 
	$content: string: text to split
	$page: page starting from 0, each page is MAX_LINE lines of text, so the start
			paragraph will be $page*MAX_LINE
out: 
	$content(too, because we don't need the original one anymore, right?)
*/

function split_content(&$content, $page)
{
	//split $content in to chunks
	//get an array of paragraphs
	$para = preg_split('~<br.*?/>|<br.*?></br>~i',$content);
	//trim out the unwanted content and make the empty element become really FALSE
	function utrim(&$str,$idx)
	{
		$str = trim($str);
	}
	array_walk($para,'utrim');
	//Throw away the empty ones
	$para = array_filter($para);
	//repair indexes
	$para = array_values($para);
	//now take the necessary content
	$display = array_slice($para, $page*MAX_LINE, MAX_LINE);
	//join it back together
	$content = join('<br />', $display);
	return count($para);
}
//Simple html simplifier for wml 1.2.x mobile wap browser
function process_content(&$content)
{
	//strip all unwanted html
	$content = strip_tags($content, '<p><b><br><a><strong><b><i><u><img>');
	//if img is big then resize it to small one (120x90) (that's my SGH-C170 screen size);
	//find all image url in $content
	preg_match_all('/<img.*?src=(".*?")/i',$content,$matches);
	$matches=str_replace('"','',$matches[1]);
	//for each url found
	foreach($matches as $img)
	{
		$resized = FALSE;
		$fn = md5($img);
		//I've tested the imagecreatefrom???() provided argument as URL, but it fails, so I must download it first
		//if we haven't download the img yet
		if(!file_exists('cache/'.$fn)) 
		{
			//stupid code to download the img, any ideas?
			$h2 = fopen('cache/'.$fn,'w');
			$h = fopen($img,'rb');
			$contents = stream_get_contents($h);
			fclose($h);
			fwrite($h2, $contents);
			fclose($h2);
		}
		define('OUT_IMG_FORMAT', IMAGETYPE_JPEG);
		$outf = "imgs/${fn}".image_type_to_extension(OUT_IMG_FORMAT);
		if(!file_exists($outf))
		{
			$info = getimagesize('cache/'.$fn); //process image
			//test the image type
			switch($info[2])
			{
				case IMAGETYPE_GIF:
					$big=imagecreatefromgif('cache/'.$fn);
					break;
				case IMAGETYPE_JPEG:
				case IMAGETYPE_JPEG2000:
					$big=imagecreatefromjpeg('cache/'.$fn);break;
				case IMAGETYPE_PNG:
					$big=imagecreatefrompng('cache/'.$fn);break;
			}
			if($info[0] >= SCREEN_WIDTH)
			{
				//resize it
				$percent=SCREEN_WIDTH/(float)$info[0];
				$small = imagecreatetruecolor(SCREEN_WIDTH, $info[1]*$percent);
				imagecopyresampled($small,$big, 0, 0, 0, 0, SCREEN_WIDTH, $info[1]*$percent, imagesx($big), imagesy($big));
				eval('image'.image_type_to_extension(OUT_IMG_FORMAT,FALSE).'($small, $outf);');  //write to file
				$resized = TRUE;
			}
			imagedestroy($big);
			imagedestroy($small);
		}
		else $resized = TRUE;
		if($resized)
			$content = preg_replace('/(<img.*?src=)("'.preg_quote($img,'/').'")(.*?>)/i','<img src="'.$outf.'" />',$content);
		else
			$content = preg_replace('/(<img.*?src=)("'.preg_quote($img,'/').'")(.*?>)/i','$1$2 />',$content);
	} //for
}

$url=urldecode($_GET['url']);
$id=$_GET['id'];
$page=$_GET['page'];
if(!$page) $page = 0;
define('MAGPIE_OUTPUT_ENCODING', 'UTF-8');
//I use magpierss for rss fetching, may be we'll start writing a simple one for outself
require_once("magpierss/rss_fetch.inc");
$rss = fetch_rss($url);
$title = $rss->items[$id]['title'];
if($rss->items[$id]['atom_content'])
	$content = $rss->items[$id]['atom_content'];
else
	$content = $rss->items[$id]['description'];
echo "<b>$id. $title</b>(trang ".($page+1).")<br />\n";
process_content($content);
$lines = split_content($content,$page);
echo $content . "<br />\n";
if(MAX_LINE*($page + 1) <= $lines)
	echo "<a href=\"viewitem.php?url=$url&id=$id&page=".($page + 1)."\">Next page</a>\n";
if(MAX_LINE*($page - 1) >= 0)
	echo "<a href=\"viewitem.php?url=$url&id=$id&page=".($page - 1)."\">Previous page</a><br />\n";
echo "<a href=\"viewitem.php?url=".urlencode($url)."&id=" . ($id+1) . "\">Next topic</a>\n";
echo "<a href=\"viewitem.php?url=".urlencode($url)."&id=" . ($id-1) . "\">Previous topic</a><br />\n";
echo "<a href=\"showtitle.php?url=".urlencode($url)."\">back to titles</a><br />\n";
?>
&#169;2010 -  Vutrankien - vukien@hcm.fpt.vn<br/>  
</p>
</card>
</wml>